package com.example.stujava.module.xiangxue.retrofit

import okhttp3.Call
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.OkHttpClient

class MyRetrofit private constructor(private val params: RetrofitParams) {

    companion object {

        fun builder(): Builder = Builder()

    }

    class Builder {
        private val params: RetrofitParams? = null
        fun baseUrl(baseUrl: String): Builder {
            params!!.baseUrl = baseUrl.toHttpUrl()
            return this
        }

        fun callFactory(factory: Call.Factory?): Builder {
            params!!.callFactory = factory
            return this
        }

        fun build(): MyRetrofit {
            checkNotNull(params!!.baseUrl) { "Base url required" }
            var callFactory: Call.Factory? = params.callFactory
            if (callFactory == null) {
                callFactory = OkHttpClient()
                params.callFactory = callFactory
            }
            return MyRetrofit(params)
        }
    }
}